//
//  VisitorView.swift
//  阿酷微博
//
//  Created by co on 16/3/12.
//  Copyright © 2016年 王南侠. All rights reserved.
//

import UIKit
import SnapKit

// 访客视图的协议
protocol VisitorViewDelegate:NSObjectProtocol {
    
    /// 访客视图注册
    func visitorViewDidRegister()
    /// 访客登录
    func visitorViewDidLogin()
}

class VisitorView: UIView {
    
    // MARK: - 代理属性
    // 提示 ： 代理一定不要忘记了 weak
    weak var delegate: VisitorViewDelegate?
    
    // MARK: - 生命周期函数
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
        
        // 添加监听方法
        registerButton.addTarget(self, action: "clickRegisterButton", forControlEvents: .TouchUpInside)
        
        loginButton.addTarget(self, action: "clickLoginButton", forControlEvents: .TouchUpInside)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  监听方法
    @objc private func clickRegisterButton(){
        delegate?.visitorViewDidRegister()
        print("点击注册-视图控制器")
        
    }
    
    @objc private func clickLoginButton(){
        delegate?.visitorViewDidLogin()
        print("点击了登录-视图控制器")
        
    }
    
    // MARK: - 公共方法
    
    func setupInfo(imageName: String? = nil, message:String) {
        
        messageLable.text = message
        
        //如果为nil 就表示首页
        guard let imageName = imageName else {
            startAnimation()
            return
        }
        
        // 不是首页需要隐藏房子
        homeIconView.hidden = true // 默认是不隐藏 true 是隐藏 逻辑要搞清楚
        maskIconView.hidden = true
        
        icoView.image = UIImage(named: imageName)
        
    }
    
    
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        // 对于需要循环播放的动画，可设置属性为false
        anim.removedOnCompletion = false
        
        icoView.layer.addAnimation(anim, forKey: nil)
    }
    

    // MARK: - 私有控件
    // 遮罩视图
    private lazy var maskIconView:UIImageView = UIImageView(cz_imageName: "visitordiscover_feed_mask_smallicon")
    /// 图标视图
    private lazy var icoView: UIImageView = UIImageView(cz_imageName: "visitordiscover_feed_image_smallicon")
    /// 小房子图标
    private lazy var homeIconView: UIImageView = UIImageView(cz_imageName: "visitordiscover_feed_image_house")
    /// 消息标签
    private lazy var messageLable : UILabel = UILabel(cz_text: "关注一些人,", aligment: .Center)
    
    /// 注册按钮
    private lazy var registerButton: UIButton = UIButton(
        cz_title: "注册",
        color: UIColor.orangeColor(),
        backImageName: "common_button_white_disable")
    
    /// 登录按钮
    private lazy var loginButton: UIButton = UIButton(
        cz_title: "登录",
        backImageName: "common_button_white_disable")

}


// MARK: - 设置界面
extension VisitorView{
    
    private func setupUI(){
//        backgroundColor = UIColor.yellowColor()
        
        // 添加控件
        addSubview(icoView)
        // 遮挡顺序不能乱写
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)

        //  2.自动布局
        //图标视图
        icoView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //小房子视图
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(icoView)
        }
        //提示文字
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(icoView)
            make.top.equalTo(icoView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
            
        }
        
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLable.snp_bottom).offset(20)
            make.left.equalTo(messageLable)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLable)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }

        // 遮罩视图
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        //  空白区域可使用背景颜色（能够使用颜色就不要使用图片）
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0) //这种数字要用这种 RGB
    }
    
}
